<script  setup>
import { reactive, ref } from 'vue'
 import ApiLogin from "../../api/login";
const ruleFormRef = ref()
import { useRouter } from 'vue-router';
const router=useRouter(); 
//引入login的接口 
//对输入的值进行判断之后 提交 返回两个token
//把这两个token存放到localstarge中
//在axios请求拦截器中加上token 
//在axios的响应器判断返回的是token过期的话 再调用更新token接口
//相当于是一个预请求


const validatePassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('Please input the password'))
  }
}

const validateUsername = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('Please input the Username '))
  } 
}
const validateUsernametest=()=>{
   console.log('getElementById()havesale  when it product sales token localstarge')
   console.log("error input the password")
}
validateUsernametest()

const ruleForm = reactive({
  passWord: '',
  userName: '',
})

const rules = reactive({
  passWord: [{ validator: validatePassword, trigger: 'blur' }],
  userName: [{ validator: validateUsername, trigger: 'blur' }],
})

const login = async () => {
  try {
    let parms={
      username:ruleForm.userName,
      password:ruleForm.passWord
    }
    console.log(parms,'parms')
    const response = await ApiLogin.toLogin(parms);
    console.log(response,'response')
    if(response.code==200){
      const { accessToken, refreshToken } = response.data // 假设返回结构中有临时token和周期token
      localStorage.setItem('accessToken', accessToken)
      localStorage.setItem('refreshToken', refreshToken)
      router.push({ name: 'home' });
    }

     //返回的两个token 和pinia结合localstarge实现本地持久化存储
     //每次都需要判断token值是否存在  在axios请求拦截器中加上token
     //在响应器中加入判断 判断返回的信息是不是token过期 如果是token过期则需要调用refreash刷新token再次执行请求
     //在refreash中重新请求成功之后再次将token存到localstarge里面去
     //需要重新去定义新的接口文案
  } catch (error) {
    console.error('Error fetching categories:', error);
  }
};
const submitForm = (formEl) => {
   
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      login()
      console.log('submit!')
    } else {
      console.log('error submit!')
    }
  })
}

const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}


</script>
<template>
    <div class="login">
          <div class="loginbox">
            <div class="left">
             logo
          </div>
          <div class="right">
            <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    status-icon
    :rules="rules"
    label-width="auto"
    class="demo-ruleForm"
  >
    <el-form-item label="Username" prop="username">
      <el-input v-model="ruleForm.userName" type="text" autocomplete="off" />
    </el-form-item>
    <el-form-item label="Password" prop="password">
      <el-input
        v-model="ruleForm.passWord"
        type="password"
        autocomplete="off"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        Submit
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
    </el-form-item>
  </el-form>
          </div>
            testtwo22
          </div>
           return testtwp
    </div>    
</template>
<style>
.login{
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}
.loginbox{
    width: 40%;
    height: 200px;
    border-radius: 8px;

}
</style>